﻿using System.Collections.Generic;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using FinPlusCommon;
using System.Xml;
using System.IO;
using Microsoft.Win32;
using FinPlusComponents;
using u = FinPlusUtility.Utility;
using log = Logger.Logger;

namespace FinPlusWPFControls
{
    public partial class FinPlusXmlEditor : FinPlusControl
    {
        public override string Id { get; set; }
        public override string[] States { get { return new string[] { }; } }
        public override string[] TestStates { get { return new string[] { }; } }
        public override IFinPlusFunctions Model { get { return _funcs; } }
        public override string ModelTest { get { return Model.ToString(); } }
        public override bool IsValid { get; set; }

        public XmlDocument xmlDocument { get { return _xmldocument; } set { _xmldocument = value; BindXMLDocument(); } }

        private enum DockControls { Functions }
        private IFinPlusFunctions _funcs;
        private XmlDocument _xmldocument;

        //construct
        public FinPlusXmlEditor()
        {
            InitializeComponent();
            IsValid = true;
        }

        //common control interface
        public override void Dock(object control, string dock)
        {
            try
            {
                switch (u.EnumTryParse<DockControls>(control.GetType().Name, DockControls.Functions))
                {
                    case DockControls.Functions: AddFunction((IFinPlusFunctions)control); break;
                }
            }
            catch (Exception e)
            {
                IsValid = log.Log(Logger.Level.Error, Id, Config, e.Message, e);
            }
        }

        public override void Initialise()
        {

        }

        public override void CommandRouter(IFinPlusFunction func)
        {
            try
            {
            
            }
            catch (Exception e)
            {
                IsValid = log.Log(Logger.Level.Warning, e.Message, e);
            }
        }

        public override void Dispose()  
        {
           
        }

        //private
        private void BindXMLDocument()
        {
            if (_xmldocument == null)
            {
                xmlTree.ItemsSource = null;
                return;
            }

            var provider = new XmlDataProvider() { Document = _xmldocument };
            var binding = new Binding() { Source = provider, XPath = "child::node()" };
            xmlTree.SetBinding(TreeView.ItemsSourceProperty, binding);
        }

        private void AddFunction(IFinPlusFunctions funcs)
        {
            _funcs = funcs;
        }
    }
}





























































































//Copyright2013<jnrton>AllRightsReserved 
//Contact j.nrton@finpus